package com.yimi.yinhepda.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.TextView;

import com.pda.ym.callbacks.UICallBack;
import com.yimi.yinhepda.R;
import com.yimi.yinhepda.db.dao.StorageInfoTableDao;
import com.yimi.yinhepda.db.table.StorageInfoTable;
import com.yimi.yinhepda.entity.request.QueryStorageInfoRequest;
import com.yimi.yinhepda.entity.response.CheckTaskResponse;
import com.pda.ym.utils.SharedPreferUtil;
import com.yimi.yinhepda.entity.response.QueryStorageInfoResponse;
import com.yimi.yinhepda.views.stockCheck_module.StockCheckUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 项目名称：YinHePda
 * 创建时间：2017/3/4 15:19
 * 版本号：1.0
 * 功能描述
 *
 * @author 刘宏恩
 */
public class CheckTaskAdapter extends BaseAdapter {

    private List<CheckTaskResponse.CheckTaskEntity> mDataList;
    private LayoutInflater inflater;

    /**
     * 记录勾选状态的集合
     */
    private List<Boolean> booleanList = new ArrayList<>();
    private SharedPreferUtil mSharedPreferUtil;
    private StorageInfoTableDao mStorageInfoTableDao;

    public CheckTaskAdapter(Context context, List<CheckTaskResponse.CheckTaskEntity> checkTaskEntities, SharedPreferUtil sharedPreferUtil) {
        super();
        this.mDataList = checkTaskEntities;
        this.mSharedPreferUtil = sharedPreferUtil;
        inflater = LayoutInflater.from(context);
        this.mStorageInfoTableDao = new StorageInfoTableDao();
        initStatus();
    }

    /**
     * 初始化勾选的状态
     */
    private void initStatus() {
        for (int i = 0; i < mDataList.size(); i++) {
            booleanList.add(false);
        }
    }

    public void set(Boolean b) {
        for (int i = 0; i < mDataList.size(); i++) {
            booleanList.set(i, b);
        }
    }

    /**
     * 获取当前位置信息
     */
    public int getCurrentPosition() {
        //设置其他的不选中
        for (int i = 0; i < mDataList.size(); i++) {
            if (mDataList.get(i).isChecked()) {
                return i;
            }
        }
        return -1;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.activity_check_task_list_item, null);
            holder = new ViewHolder();
            holder.linearLayout = convertView.findViewById(R.id.check_task_list_item);
            //盘点单号
            holder.checkBillNo = convertView.findViewById(R.id.check_bill_no);
            //盘点库区
            holder.area = convertView.findViewById(R.id.check_arrea);
            //票数
            holder.stockNum = convertView.findViewById(R.id.check_price_no);
            //状态
            holder.status = convertView.findViewById(R.id.check_status);
            holder.radioButton = convertView.findViewById(R.id.check_task_radio_button);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        final CheckTaskResponse.CheckTaskEntity item = mDataList.get(position);
        //盘点单号
        holder.checkBillNo.setText(item.getStockNo());
        //库区名称
        String compCode = mSharedPreferUtil.getCompanyCode();
        String deptCode = mSharedPreferUtil.getDeptCode();
        String storageCode = item.getStockArea();

        QueryStorageInfoRequest request = new QueryStorageInfoRequest();
        request.setDeptCode(deptCode);
        request.setStorageCode(storageCode);
        StockCheckUtil.queryStorageInfo(request, new UICallBack() {
            @Override
            public void onError(String msg) {

            }

            @Override
            public void onSuccess(Object obj) {
                QueryStorageInfoResponse response = (QueryStorageInfoResponse) obj;
                if (response.getData() != null) {
                    QueryStorageInfoRequest info = response.getData().get(0);
                    holder.area.setText(info.getStorageName());
                } else {
                    holder.area.setText("");
                }
            }
        });

        //票数
        holder.stockNum.setText(String.format("%s", item.getStockNum()));
        //状态
        Integer stockStatus = item.getStockStatus();
        switch (stockStatus) {
            case 1:
                holder.status.setText("未盘点");
                break;
            case 2:
                holder.status.setText("盘点中");
                break;
            case 3:
                holder.status.setText("已盘点");
                break;
            default:
                break;
        }

        //设置是否选中
        if (item.isChecked()) {
            holder.radioButton.setChecked(true);
        } else {
            holder.radioButton.setChecked(false);
        }

        holder.linearLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                //设置其他的不选中
                for (CheckTaskResponse.CheckTaskEntity item : mDataList) {
                    item.setChecked(false);
                }
                item.setChecked(true);
                notifyDataSetChanged();
            }
        });

        //勾选
        holder.radioButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                for (CheckTaskResponse.CheckTaskEntity item : mDataList) {
                    item.setChecked(false);
                }
                item.setChecked(true);
                notifyDataSetChanged();
            }
        });
        return convertView;
    }

    private static class ViewHolder {
        LinearLayout linearLayout;
        TextView checkBillNo;
        TextView area;
        TextView stockNum;
        TextView status;
        RadioButton radioButton;
    }

    @Override
    public int getCount() {
        return mDataList.size();
    }

    @Override
    public Object getItem(int position) {
        return mDataList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }
}
